# Routing by name convention
class PostsController < ServerSide::ActionController
  # route is /posts/:action/:id by default
  # the route creates two instance variables: @action, @id
  # invoke => :action
  # view rendering is implicit
  
  default_action :list
  
  def list
    @posts = Post.find :all
  end
  
  def edit
    @post = Post.find(@id)
  end 
end

# An advanced rails-style controller
class AdvancedController < ServerSide::ActionController ['/:action', '/advanced/:action']
  default_action :home
  
  def home
    @latest = Posts.find :all, :order => 'time DESC', :limit => 10
  end
  
  def about
  end
  
  def faq
  end
end

# A REST controller
class PostController < ServerSide::RESTController('/:id')
  # :invoke => :method
  def get
    @post = Post.find(@id)
    render_view 'node_state'
  end
  
  def post
    if @id
      post = Post.find(@id)
    else
      post = Post.create
    end
    post.title = @params[:title]
    post.body = @params[:body]
    post.save
    
    redirect_to "/#{post.id}"
  end
end

# Custom routing ###############################################3

# The following two examples will require refactoring of the static serving code.
ServerSide.route(:path => '/img') do
  send_file('./static/img'/@params[:id], :disposition => :inline)
end

ServerSide.route(:host => /^static\..+/) do
  serve_static('./static'/@path, false) # the second parameter is whether to
    # allow directory listings. If it is ommited, the default is used.
end

# Deal with stuff like /favicon.ico and /robots.txt
ServerSide.route(:path => /^\/(.*)\.(.*)$/) do
  serve_static('./static'/@path)
end

# Redirect
ServerSide.route(:path => '/xml/:flavor/feed.xml') do
  redirect('http://feeds.feedburner.com/RobbyOnRails')
end

# an aside: instance_eval

p = Proc.new {puts @test}

class C
  def test(arg, &block)
    @test = arg
    instance_eval(&block)
  end
end

C.new.test(123, &p) #=> 123
